const db = require('../config/db.js');
const inFile = require('../utils/inFile.js');

const informationModel = {
    //带分页查询,模糊查询
    GetInformationList: async ({pageCurrent,pageSize,InformationKeyword}) =>{
        console.log(pageCurrent,'当前页');
        console.log(pageSize,'每页显示的条数');
        let current = (pageCurrent - 1) * pageSize || 0;
        let size = pageSize || 10;
        let sql1 = 'SELECT COUNT(*) AS total FROM information;';
        let sql2 = `SELECT COUNT(*) AS total FROM information WHERE CONCAT(title,content) LIKE CONCAT('%', ?, '%')`;
        let sql3 = `SELECT id,title,content,image,create_time,state FROM information LIMIT ${current},${size}`;
        let sql4 = `SELECT id,title,content,image,create_time,state FROM information WHERE CONCAT(title,content) LIKE CONCAT('%', ?, '%') LIMIT ${current},${size};`
        try {
            if(InformationKeyword) {

                const res1 = await db.query(sql4, [InformationKeyword]);
                const res2 = await db.query(sql2,[InformationKeyword]);
                const [{total}]= res2[0];
                console.log(total, '获取资讯列表总记录');
                console.log(res1[0], '获取资讯列表记录分页带模糊查询');
                return {
                    list: res1[0],
                    total,
                }
            }else{
                const res1 = await db.query(sql1);
                const res2 = await db.query(sql3);
                const [{total}]= res1[0];
                console.log(total, '获取资讯列表总记录');
                console.log(res2[0], '获取资讯列表记录分页');
                return {
                    total,
                    list: res2[0]
                }
            }
        } catch (error) {
            console.log(error,'model层获资讯列表数据出错啦');
        }
    },
    AddInformation:async({title,content,create_time,state,image},file)=>{
        const sql1 = "SELECT * FROM information WHERE title = ? ";
        const sql2 = "INSERT INTO information (title,content,create_time,state,image) VALUES (?,?,?,?,?);";
        const sql3 = "INSERT INTO information (title,content,create_time,state) VALUES (?,?,?,?);";
        try {
            if (file) {
                const imgPath = inFile(file,'img/Information/');
                if (title) {
                    const result1 = await db.query(sql1, [title]);
                    console.log(result1[0], 'result1[0]');
                    if (result1[0].length !== 0) {
                        return 0;
                    } else {
                        const result2 = await db.query(sql2, [title,content,create_time,state,imgPath]);
                        console.log(result2[0], 'result2[0]');
                        return 1;
                    }
                }
            }else{
                if (title) {
                    const result1 = await db.query(sql1, [title]);
                    console.log(result1[0], 'result1[0]');
                    if (result1[0].length !== 0) {
                        return 0;
                    } else {
                        const result2 = await db.query(sql3, [title,content,create_time,state]);
                        console.log(result2[0], 'result2[0]');
                        return 1;
                    }
                }
            }
        } catch (error) {
            console.log(error, 'Information添加资讯失败啦');
        }
    },
    EditInformation: async ({id,title,content,create_time,state,image},file) => {
        const sql1 = "UPDATE information SET title=?,content=?,create_time=?,state=?,image=? WHERE id=?;";
        const sql2 = "UPDATE information SET title=?,content=?,create_time=?,state=? WHERE id=?;";
        try {
            if (file) {
                const imgPath = inFile(file,'img/Information/');
                if (id) {
                    const result2 = await db.query(sql1, [title,content,create_time,state,imgPath,id]);
                    console.log(result2[0], 'result2[0]');
                    return 1;
                }else{
                    return 2
                }
            }else{
                if (id) {
                    const result2 = await db.query(sql2, [title,content,create_time,state,id]);
                    console.log(result2[0], 'result2[0]');
                    return 1;
                }else{
                    return 2
                }
            }
        } catch (error) {
            console.log(error, 'information修改资讯失败啦');
        }
    },
    UpdateInformationState: async ({id,state})=> {
        console.log(id,state,'-----');
        const sql = 'UPDATE information SET state=? WHERE id=?;';
        try {
            if(id){
                const res = await db.query(sql,[state,id]);
                console.log(res[0],'更新资讯状态调试');
                return res[0];
            }else{
                return;
            }
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    },
    DeleteInformation: async ({id})=>{
        console.log(id,'删除资讯id测试');
        const sql ='DELETE FROM information WHERE id=?;';
        try {
            if(id){
                const res = await db.query(sql,[id]);
                console.log(res[0],'删除资讯调试');
                return res[0];
            }else{
                return;
            }
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    },
    BatchDeleteInformation: async  ({idList}) => {
        console.log(idList,'收集idList的数组');
        const sql = `DELETE FROM information WHERE id IN(${idList});`;
        try {
            if (idList.length>0){
                const res = await db.query(sql);
                console.log(res,'Model层批量删除资讯调试');
                return res[0];
            }else{
                return ;
            }
        }catch (error){
            return{
                errorMsg:error
            }
        }
    },
    GetInformationDetail:async ({id}) => {
        console.log(id,'资讯id');
        const sql = 'SELECT title,content,create_time,state,image FROM information WHERE id = ? ;';
        try {
            if(id){
                const res = await db.query(sql,[id]);
                console.log(res[0],'获取资讯详情调试');
                return res[0];
            }else{
                return;
            }
        }catch (error) {
            console.log(error,'获取资讯详情出错了——Model层');
        }
    },
    GetInformationPublishList: async ({pageCurrent,pageSize,InformationKeyword}) =>{
        console.log(pageCurrent,'当前页');
        console.log(pageSize,'每页显示的条数');
        let current = (pageCurrent - 1) * pageSize || 0;
        let size = pageSize || 10;
        let sql1 = 'SELECT COUNT(*) AS total FROM information WHERE state = 1 ;';
        let sql2 = `SELECT COUNT(*) AS total FROM information WHERE CONCAT(title,content) LIKE CONCAT('%',?, '%')  AND state = 1`;
        let sql3 = `SELECT id, title, content, image, create_time, state FROM information WHERE state = 1 LIMIT ${current},${size};`;
        let sql4 = `SELECT id,title,content,image,create_time,state FROM information WHERE CONCAT(title,content) LIKE CONCAT('%',?, '%')  AND state = 1  LIMIT ${current},${size};`
        try {
            if(InformationKeyword) {

                const res1 = await db.query(sql4, [InformationKeyword]);
                const res2 = await db.query(sql2,[InformationKeyword]);
                const [{total}]= res2[0];
                console.log(total, '获取资讯列表总记录');
                console.log(res1[0], '获取资讯列表记录分页带模糊查询');
                return {
                    list: res1[0],
                    total,
                }
            }else{
                const res1 = await db.query(sql1);
                const res2 = await db.query(sql3);
                const [{total}]= res1[0];
                console.log(total, '获取资讯列表总记录');
                console.log(res2[0], '获取资讯列表记录分页');
                return {
                    total,
                    list: res2[0]
                }
            }
        } catch (error) {
            console.log(error,'model层获资讯列表数据出错啦');
        }
    },
};


module.exports = informationModel;